package dstree

type Dstree_Node struct {
	//=====node info
	Level               int64             //节点深度
	Split_Policy        Node_Split_Policy //分裂策略
	Node_Points_num     int16
	Node_Points         []int16          //分段右端点
	Segment_Sketches    []Segment_Sketch //每段的Sketch
	Is_leaf             bool             //是否为叶节点
	Is_left             bool             //是否为父节点的左孩子
	Node_Size           int32            //子树存ts的数量
	Hs_split_points_num int16
	Hs_split_points     []int16
	Hs_Segment_Sketches []Segment_Sketch
	//=====node info
	Name        string       //方便记录
	data        *Timeseries  //叶节点存的数据
	Left_child  *Dstree_Node //左孩子
	Right_child *Dstree_Node //右孩子
	Parent      *Dstree_Node //父节点
	Digest      []byte       //哈希值
}

func (node *Dstree_Node) Node_init_segments(points []int16, segment_size int) {

	node.Node_Points = points
	//把segment又平分了一遍
	min_length := 1
	node.Hs_split_points = Calc_hs_split_points(points, segment_size, min_length)
	//初始化sketches
	node.Segment_Sketches = make([]Segment_Sketch, segment_size)
	node.Hs_Segment_Sketches = make([]Segment_Sketch, len(node.Hs_split_points))
	//allocate memory for vertical Indicators
	node.Node_Points_num = int16(segment_size)
	for i := 0; i < segment_size; i++ {
		node.Segment_Sketches[i].Indicators = make([]Ts_type, 4)
		node.Segment_Sketches[i].Indicators[0] = MintsType
		node.Segment_Sketches[i].Indicators[1] = MaxtsType
		node.Segment_Sketches[i].Indicators[2] = MintsType
		node.Segment_Sketches[i].Indicators[3] = MaxtsType
	}
	//allocate memory for horizontal Indicators
	num_hs_node_points := len(node.Hs_split_points)
	node.Hs_split_points_num = int16(num_hs_node_points)
	// fmt.Println(node.Hs_split_points_num)
	for i := 0; i < num_hs_node_points; i++ {
		node.Hs_Segment_Sketches[i].Indicators = make([]Ts_type, 4)
		node.Hs_Segment_Sketches[i].Indicators[0] = MintsType
		node.Hs_Segment_Sketches[i].Indicators[1] = MaxtsType
		node.Hs_Segment_Sketches[i].Indicators[2] = MintsType
		node.Hs_Segment_Sketches[i].Indicators[3] = MaxtsType
	}
}

func (node *Dstree_Node) update_node_statistics(timeseries []Ts_type) {
	//update vertical node_segment_sketch
	num_node_points := len(node.Node_Points)
	for i := 0; i < num_node_points; i++ {
		from := Get_Segment_Start(node.Node_Points, i)
		to := Get_Segment_End(node.Node_Points, i)

		Node_segment_sketch_update_sketch(&node.Segment_Sketches[i], timeseries, int16(from), int16(to))

	}
	//update horizontal node_segment_sketch
	hs_num_node_points := len(node.Hs_split_points)
	for i := 0; i < hs_num_node_points; i++ {
		from := Get_Segment_Start(node.Hs_split_points, i)
		to := Get_Segment_End(node.Hs_split_points, i)
		Node_segment_sketch_update_sketch(&node.Hs_Segment_Sketches[i], timeseries, int16(from), int16(to))
	}
	//数量
	node.Node_Size++
}

func (index *Dstree_Index) create_child_node(node *Dstree_Node) *Dstree_Node {

	child_node := index.dstree_leaf_node_init(node.Level + 1)
	child_node.Parent = node
	child_node.Is_leaf = true

	return child_node
}
